package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.CardService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.CardVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CardManager {

    @DubboReference
    private CardService cardService;

    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 卡片展示
     * @return
     */
    public ResponseEntity cards() {
        //获取线程内的id
        Long userId = UserHolder.get().getId();
        //获取推荐给我的用户
        List<RecommendUser> recommendUserList = cardService.findByRecommendUserCard(userId);

        //3.遍历集合,获取每个推荐用户的id,然后获取userInfo,封装CardVo对象
        ArrayList<CardVo> voList = new ArrayList<>();
        //判断recommendUserList是否为空
        if (CollUtil.isNotEmpty(recommendUserList)) {
            //遍历recommendUserList
            recommendUserList.forEach(r->{
                //获取推荐用户的详细信息
                UserInfo userInfo = userInfoService.findByUserId(r.getUserId());

                //创建CardVo封装userInfo
                CardVo cardVo = new CardVo();

                cardVo.setId(userInfo.getId().intValue()); //id
                cardVo.setAvatar(userInfo.getAvatar()); //头像
                cardVo.setNickname(userInfo.getNickname()); //昵称
                cardVo.setGender(userInfo.getGender()); //性别
                cardVo.setAge(userInfo.getAge()); //年龄

                String[] tags = userInfo.getTags().split(",");
                cardVo.setTags(tags);

                voList.add(cardVo);
            });
        }
        return ResponseEntity.ok(voList);
    }

    /**
     * 卡片_左划不喜欢
     */
    public void noLikeCard(Integer noLikeId) {
        //获取线程id
        Long userId = UserHolder.get().getId();

        //调用CardService的noLikeCard方法
        cardService.noLikeCard(userId,noLikeId);
    }

    /**
     * 卡片_右划喜欢
     */
    public void likeCard(Integer likeId) {
        //获取线程id
        Long userId = UserHolder.get().getId();

        //调用CardService的LikeCard方法
        cardService.LikeCard(userId,likeId);
    }
}
